package swimv2gm.client.user;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2gm.bl.session.FriendshipManager;

/**
 * Servlet implementation class ManageFriends
 */
public class ProcessFriendship extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		FriendshipManager man;
		try {
			man = (FriendshipManager)InitialContext.doLookup("FriendshipManager");
		} catch (NamingException e) {
			response.sendError(500);
			return;
		}

		Long myId = (Long)(request.getSession().getAttribute("user"));
		if (myId == null) {
			response.sendError(403);
			return;
		}

		boolean result;
		long mitt;
		try {
			mitt = Long.parseLong(request.getParameter("mitt"));
			boolean accepted = Boolean.parseBoolean(request.getParameter("accept"));
			result = man.processRequest(mitt,myId,accepted);
		} catch (IllegalArgumentException e) {
			result = false;
		}

		response.sendRedirect("/SWIMv2gm/user/FriendList?friendship=" + Boolean.toString(result));
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		doGet(request,response);
	}

}
